/**
  ******************************************************************************
  * @file    main.c 
  * @author  caizhiwei
  * @version V1.0
  * @Board   stm32f103r8t6
  * @date    2014/07/02
  * @http: 

  ******************************************************************************
 */


#include "calendar.h"
#include "delay.h"

#include "bsp_coreboard_led.h"
#include "timer.h"
#include "chipid.h"
#include "draw.h"   

#include "bsp_ds18b20.h"



void NVIC_Configuration(void);
void time_display(void);
void  Com1_Adj_time();
void disp_sysinfo(void);

void main(void)
{
   
  NVIC_Configuration();
  Delay_Init(72);
  USART1_Init(115200);
  Get_ChipID();	
  Get_Flash_Size();
    
  RTC_Init();
  Timer2_Init();
  
 // SPI1_Init();
 // SPI1_DMA_Configuration();
  
    LED_Init();
    Get_ChipID();
    Get_Flash_Size();	
	
    LCD_Init();     
    BLK_ON;  
  
    LCD_Disp_Image(Logo);
	delay_ms(1000); 
    LCD_Clear_GDRAM(); 
    
    disp_sysinfo();
    delay_ms(1500); 
    
    LCD_Clear_DDRAM();
    
    LCD_Write_USR_CGRAM();
    ShowCharPos(0, 0,0x0e);  // Music 
    
  while(1)
  {

   time_display();
   Com1_Adj_time();

   //LCD_DISP_USR_Char(2, 0,2);  //clock icon on
   // delay_ms(500);      
    //printf("%d\n",real_tim_acc_sec);
  
  //  printf("######## %d\r\n",get_daytime());
  //printf("%d\n",get_daytime());
  // printf("%d\n",time_acc_sec);
   
    
  }
}


void disp_sysinfo(void)
{

  ShowStringPos(0, 0,"iar770 ppm:32"); // 
  ShowStringPos(1, 0,"I love ZhuZhu");
  ShowStringPos(2, 0,"Hello,Kitty!");
  ShowStringPos(3, 1,__DATE__);    //代码烧录日期

}


/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
   

  NVIC_InitTypeDef NVIC_InitStructure;
  
    #ifdef  VECT_TAB_RAM  
            /* Set the Vector Table base location at 0x20000000 */ 
            NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
    #else  /* VECT_TAB_FLASH  */
            /* Set the Vector Table base location at 0x08000000 */ 
            NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
    #endif
            
    /* Configure one bit for preemption priority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    //=~1  0~7  

    NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure); 

	
}

struct tm* tm_t;

void time_display()
{

   char mon_str[4]="/";
   char mon_str1[4]="/";
   char temp_mon_str[2]="0";
   char day_str[3],day_str1[3]="0";
   u8 Nonglistr[15];
   u8 JieQiStr[15]="  今天是";
   //日历时间转成本地tm结构时间  
	tm_t = localtime(&real_tim_acc_sec); 

	ShowNumPos(3, 0, (tm_t->tm_year+1900),10);
	
	sprintf(mon_str,"%d",(tm_t->tm_mon+1)); 
    if(strlen(mon_str)==1) // month < 10, add "0" in front
		strcat (temp_mon_str, mon_str);	//07
	else
		strcpy (temp_mon_str, mon_str);	//07
				
	strcat ( mon_str1,temp_mon_str);	// -07
	strcat ( mon_str1,"/");	// -07-

	ShowStringPos(3,2,mon_str1);   //-07-
	
	if(tm_t->tm_mday <10)
	{
		sprintf(day_str,"%d",tm_t->tm_mday); 		
		strcat (day_str1, day_str);    //06
		ShowStringPos(3,4,day_str1);   //06
	}
	else
	ShowNumPos(3, 4, tm_t->tm_mday,10);

	ShowGBStringPos(3, 5, "星期");

        switch(tm_t->tm_wday)
        {
        case  0 :
            ShowGBPos(3, 7, "日"); break;   
        case 1 : 
          ShowGBPos(3, 7, "一"); break;
        case 2 :
          ShowGBPos(3, 7, "二");break;
        case 3 :
          ShowGBPos(3, 7, "三"); break;
        case 4 :
          ShowGBPos(3, 7, "四"); break;
        case 5 :
          ShowGBPos(3, 7, "五"); break;
        case 6 :
          ShowGBPos(3, 7, "六");;break;
                
        }

	    Show2NumPos(1, 7, tm_t->tm_sec);
		
		GetChinaCalendarStr((tm_t->tm_year+1900),(tm_t->tm_mon+1),tm_t->tm_mday,Nonglistr);
		GetJieQiStr((tm_t->tm_year+1900),(tm_t->tm_mon+1),tm_t->tm_mday,JieQiStr);

		
		if(tim2_flag)
		{

			ShowBlankPos(2, 0, 2);	// clear li
			ShowGBStringPos(2, 1, Nonglistr+6); //偏移3个汉字
			ShowBlankPos(2, 5, 2); // clear 15
			ShowNumPos(2, 6, 24,10);  // 24
			ShowGBPos(2, 7, "℃");

		}
		else
		{

		   ShowBlankPos(2, 6, 4);  //clear 24 sheshidu
		   ShowGBStringPos(2, 0, JieQiStr); 	  
		   
		}
		
		
	  LCD_Disp_Large_Num1632(digitalFont_16x32,1,(tm_t->tm_hour)/10,false); //
	  LCD_Disp_Large_Num1632(digitalFont_16x32,2,(tm_t->tm_hour)%10,false); //

	  LCD_Disp_Large_Num1632(digitalFont_16x32,3,10,false); //:

	LCD_Disp_Large_Num1632(digitalFont_16x32,4,(tm_t->tm_min)/10,false); //
    LCD_Disp_Large_Num1632(digitalFont_16x32,5,(tm_t->tm_min)%10,false); //
	

    LCD_WriteCMD(0x30);  //绘图显示关，基本指令集
    delay_us(7); 


}


// AABB20150808201809FF  // sent
//FF AA BB 20 15 08 02 18 55 49  //Received 
//【2015-08-02 19:38:12:152】AA BB 20 15 08 02 18 55 49 FF 
//【2015-08-02 19:38:12:561】AA BB 20 15 08 02 18 55 49 FF 
//【2015-08-02 19:38:12:963】AA BB 20 15 08 02 18 55 49 FF 
//【2015-08-02 19:38:13:385】AA BB 20 15 08 02 18 55 49 FF 

 void Com1_Adj_time()
{

if(uart_full_flag)
{
        //USART_Send_Array(USART1,uart1_buff,10) ; //debug
     
	uart_full_flag = false;
    if(uart1_buff[0] =0xaa && uart1_buff[1]==0xbb )
    {
        
        for(u8 i=2;i<9;i++)  //2,3,4,5,6,7,8
        {
        uart1_buff[i] = uart1_buff[i]/16*10 + uart1_buff[i]%16;  //hex to dec
        }
 
	tm_t->tm_year =  uart1_buff[2]*100+uart1_buff[3] -1900;  //115
            
	tm_t->tm_mon = (int)(uart1_buff[4]) -1; //0~11         
	tm_t->tm_mday = (int)(uart1_buff[5]);
	
	 tm_t->tm_hour = (int)(uart1_buff[6]);
	 tm_t->tm_min = (int)(uart1_buff[7]);
	 tm_t->tm_sec = (int)(uart1_buff[8]);
         
         real_tim_acc_sec = mktime(tm_t);  // must modify 
         
         printf(ctime(&real_tim_acc_sec)); 
	
#if 1
	 /* Enable PWR and BKP clocks */
         RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); 
         
         /* Allow access to BKP Domain */
         PWR_BackupAccessCmd(ENABLE);      
         RTC_SetCounter(real_tim_acc_sec);   
          /* Wait until last write operation on RTC registers has finished */
         RTC_WaitForLastTask();
         
         PWR_BackupAccessCmd(DISABLE);//禁止RTC后备寄存器 
#endif
        
     }
        
   }
 
}
  
